package cq.zhcqk.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;

/**
 * Created by zhangle on 2017/12/10.
 */
public class SQ_Yjjy_SubActivity extends BaseActivity implements View.OnClickListener{

    private EditText title;
    private EditText neirong;
    private EditText name;
    private EditText phone;
    private EditText idno;
    private Button sub;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_sq_sub);
        initBack();
        title = (EditText) findViewById(R.id.title);
        neirong = (EditText) findViewById(R.id.neirong);
        name = (EditText) findViewById(R.id.name);
        phone = (EditText) findViewById(R.id.phone);
        idno = (EditText) findViewById(R.id.idno);
        sub = (Button) findViewById(R.id.sub);
        sub.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("我要写信");
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
            case R.id.sub:
                submit();
                break;
        }
    }

    private void submit(){
        String title1 = title.getText()+"";
        String content1 = neirong.getText()+"";
        String name1 = name.getText()+"";
        String phone1 = phone.getText()+"";
        String idcard1 = idno.getText()+"";
        if(title1==null || "".equals(title1)){
            Toast.makeText(context,"请输入标题",Toast.LENGTH_SHORT).show();
            return;
        }
        if(content1==null || "".equals(content1)){
            Toast.makeText(context,"请输入内容",Toast.LENGTH_SHORT).show();
            return;
        }
        if(name1==null || "".equals(name1)){
            Toast.makeText(context,"请输入姓名",Toast.LENGTH_SHORT).show();
            return;
        }
        if(phone1==null || "".equals(phone1)){
            Toast.makeText(context,"请输入电话",Toast.LENGTH_SHORT).show();
            return;
        }
        if(idcard1==null || "".equals(idcard1)){
            Toast.makeText(context,"请输入身份证号",Toast.LENGTH_SHORT).show();
            return;
        }
        RequestParams params = new RequestParams();
        params.add("title", title1);
        params.add("content", content1);
        params.add("name",name1);
        params.add("phone", phone1);
//        params.add("idcard", idcard1);
        params.add("idcard", "123");
        httpClient.post(Common.URL+Common.SAVESUGGESTION,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                ArrayList<Map<String, Object>> listNew = null;
                listNew = (ArrayList<Map<String, Object>>)map.get("data");
                Toast.makeText(context,"提交成功",Toast.LENGTH_SHORT).show();
                finish();
//                if(listNew == null || listNew.size()==0){
//                    Toast.makeText(context,"没有更多",Toast.LENGTH_SHORT).show();
//                }else{
//                    list.addAll(listNew);
//                }
//                rcv.completeRefresh();
//                rcv.completeLoad();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("errorResponse："+errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                System.out.println("responseString："+responseString);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }
}
